package com.liangsui.mss.srevice.impl;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liangsui.mss.domain.Scheduling;
import com.liangsui.mss.dto.SchedulingFormDto;
import com.liangsui.mss.dto.SchedulingQueryDto;
import com.liangsui.mss.mapper.SchedulingMapper;
import com.liangsui.mss.service.SchedulingService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author zhang
* @description 针对表【his_scheduling(排班信息表)】的数据库操作Service实现
* @createDate 2022-12-06 22:36:39
*/
@DubboService(timeout = 5000)
public class SchedulingServiceImpl extends ServiceImpl<SchedulingMapper, Scheduling>
    implements SchedulingService{

    @Autowired
    private SchedulingMapper schedulingMapper;

    @Override
    public List<Scheduling> queryScheduling(SchedulingQueryDto schedulingQueryDto) {
        QueryWrapper<Scheduling> wrapper = new QueryWrapper<>();
        wrapper.eq(null != schedulingQueryDto.getUserId(),
                Scheduling.COL_USER_ID, schedulingQueryDto.getUserId());
        wrapper.eq(null != schedulingQueryDto.getDeptId(),
                Scheduling.COL_DEPT_ID, schedulingQueryDto.getDeptId());
        wrapper.ge(StringUtils.isNotBlank(schedulingQueryDto.getBeginDate()),
                Scheduling.COL_SCHEDULING_DAY, schedulingQueryDto.getBeginDate());
        wrapper.le(StringUtils.isNotBlank(schedulingQueryDto.getEndDate()),
                Scheduling.COL_SCHEDULING_DAY, schedulingQueryDto.getEndDate());
        return this.schedulingMapper.selectList(wrapper);
    }

    @Override
    public int saveScheduling(SchedulingFormDto schedulingFormDto) {
        // 保存条件
        if (null != schedulingFormDto.getData() && schedulingFormDto.getData().size() > 0) {
            DateTime dateTime = DateUtil.parse(schedulingFormDto.getBeginDate(), "yyyy-MM-dd");
            // 得到beginDate所在周的周一
            DateTime date = DateUtil.beginOfWeek(dateTime);
            String beginDate = DateUtil.format(date, "yyyy-MM-dd");
            String endDate = DateUtil.format(DateUtil.endOfWeek(dateTime), "yyyy-MM-dd");
            // 得到用户名和科室
            SchedulingFormDto.SchedulingData schedulingData = schedulingFormDto.getData().get(0);
            Long userId = schedulingData.getUserId();
            Long deptId = schedulingData.getDeptId();
            if (null != userId) {
                // 删除原来的这个用户的当前周的所有排班数据
                QueryWrapper<Scheduling> wrapper = new QueryWrapper<>();
                wrapper.eq(Scheduling.COL_USER_ID, userId);
                wrapper.eq(Scheduling.COL_DEPT_ID, deptId);
                wrapper.between(Scheduling.COL_SCHEDULING_DAY, beginDate, endDate);
                this.schedulingMapper.delete(wrapper);
                // 在进行保存,添加新的排班
                // 初始化当前周一到周日的日期数据
                List<String> schedulingDays = initSchedulingDay(date);
                for (SchedulingFormDto.SchedulingData d : schedulingFormDto.getData()) {
                    Scheduling scheduling = null;
                    int i = 0;
                    for (String s : d.getSchedulingType()) {
                        if (StringUtils.isNotBlank(s)) {
                            scheduling = new Scheduling(userId, deptId, schedulingDays.get(i),
                                    d.getSubsectionType(), s, new Date(), schedulingFormDto.getSimpleUser().getUserName());
                            this.schedulingMapper.insert(scheduling);
                        }
                        i++;
                    }
                }
                return 1;// 保存成功
            } else {
                return 0;// 用户为空,保存失败
            }
        }
        return 0;// 保存失败
    }

    /**
     * @Description: 查询有医生排版的科室
     * @Param:
     * @return:
     */
    @Override
    public List<Long> queryDeptIdsForScheduling(Long deptId, String subsectionType, String schedulingType, String schedulingDay) {
        Wrapper<Scheduling> wrapper=new QueryWrapper<>();
        ((QueryWrapper<Scheduling>) wrapper).eq(null != deptId,
                Scheduling.COL_DEPT_ID,deptId);
        ((QueryWrapper<Scheduling>) wrapper).eq(StringUtils.isNotBlank(subsectionType),
                Scheduling.COL_SUBSECTION_TYPE,subsectionType);
        ((QueryWrapper<Scheduling>) wrapper).eq(StringUtils.isNotBlank(schedulingType),
                Scheduling.COL_SCHEDULING_TYPE,schedulingType);
        ((QueryWrapper<Scheduling>) wrapper).eq(StringUtils.isNotBlank(schedulingDay),
                Scheduling.COL_SCHEDULING_DAY,schedulingDay);
        List<Scheduling> schedulings = this.schedulingMapper.selectList(wrapper);
        List<Long> longs=new ArrayList<>();
        longs.clear();
        for (Scheduling scheduling : schedulings) {
            longs.add(scheduling.getDeptId());
        }
//        List<Long> longs = this.schedulingMapper.queryDeptIdsForScheduling(deptId, subsectionType, schedulingType, schedulingDay);
        return longs;
    }

    @Override
    public List<Scheduling> selectAll2() {
        return schedulingMapper.selectAll2();
    }

    /**
     * 修改挂号状态为已收费
     * @param regId
     * @return
     */
//    @Override
//    public int saveCharge(String regId) {
//        return 0;
//    }

    /**
     * @Description: 根据开始时间构造一周的日期数组
     * @Param: beginTime
     * @Return: List<String>
     */
    private List<String> initSchedulingDay(Date beginTime) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            DateTime dateTime = DateUtil.offsetDay(beginTime, i);// 偏移天数，正数向未来偏移，负数向历史偏移
            String key = DateUtil.format(dateTime, "yyyy-MM-dd");
            list.add(key);
        }
        return list;
    }
}




